<?php
namespace common\components\services\goods;

use common\components\helpers\Fun;
use common\components\helpers\Util;
use common\models\datas\Brand;
use common\models\datas\Cate;
use frontend\models\datas\Arrangement;
use frontend\models\datas\Goods;
use frontend\models\datas\GoodsClassTime;
use frontend\models\datas\Mechanic;
use \yii\base\Action;
use yii\db\Query;
use yii\web\BadRequestHttpException;

/**
 * 商品详情显示的独立方法
 */
class DetailsAction extends Action{

    public $default_url='@common/components/services/goods/CommondetailView.php';
    public $type;
    public $goodid;

    public function run(){
//        $this->controller->layout='@frontend/views/layouts/iframe';
        if(\Yii::$app->request->get('id') == null){
            throw new BadRequestHttpException(\Yii::t('frontend','商品id为空'));
        }
        $this->goodid = \Yii::$app->request->get('id');
        $site_id = Fun::getSiteId();
        $good = Goods::findOne($this->goodid);
        if(\Yii::$app->request->get('type_id') == null){
            $this->type = $good->type_id;
        }else{
            $this->type = \Yii::$app->request->get('type_id');
        }
        $category=null;$brand=null;$mechanic=null;
        if($good->cat_id != null){//分类id不为空
            $category = Cate::findOne($good->cat_id);
        }
        if($good->brand_id != null){
            $brand = Brand::findOne($good->brand_id);
        }
        if($good->mechanic_id != null){
            $mechanic = Mechanic::findOne(['id'=>$good->mechanic_id,'site_id'=>$site_id]);
        }
        /**
         * 1. 根据商品id获得了商品记录 ok
         * 2. 商品记录关联的商品图片显示，最大购买量，赠送积分，库存警告，无库存是否可购买，是否可购买，是否显示，上下架 ok 后期前端显示页面隐藏不想要的
         * 3. 扩展属性 ok
         * 4. 课程特有的：教师，课程，教室编码，上课时间-(星期，开始时间，结束时间)，开班人数，上课次数，最满人数，是否可预约，预约价格，可预约课次
         */
        /**获得商品分类名称,暂时无用
        if($this->type == 1){//课程商品
            $goodcate_str = '课程';
        }else{//其他商品根据分类id获取
            $cate = Cate::findOne($good->cat_id);
            $goodcate_str = $cate->name;
        }*/
        //获取课程或舞台类型特有的数据们
        $specialdatas= $this->getSpecialModuleDatas($this->type, $this->goodid);
        //获取商品扩展属性显示
        $attributedetail = Fun::getExtendattributeDetails($this->type, $this->goodid);
        //'tree'=>$tree,'brand'=>$brand,'mechanic'=>$mechanic,
        return $this->controller->render('details',['model'=>$good,'specialdatas'=>$specialdatas,'attributedetail'=>$attributedetail,'default'=>\Yii::$app->view->render($this->default_url,['model'=>$good,'category'=>$category,'brand'=>$brand,'mechanic'=>$mechanic,'attributedetail'=>$attributedetail])]);
    }

    //获取某种类型商品特有的扩展数据
    public function getSpecialModuleDatas($typeid, $goodid){
        if($goodid == null || $typeid == null){
            return null;
        }
        $good = Goods::findOne($goodid);
        if(empty($good)){
            return null;
        }
        if($typeid == 1){//课程产品
                //$arrange = Arrangement::findOne($good->arrangement_id)->toArray([],['course_name','teacher_name','room_number','class_day','start_time','end_time']);//根据排课id获取排课记录
                $goodsarr_query = (new Query())->select(["goods_id","max(IF(name='课程',value,'')) as course_name","max(IF(name='老师',value,'')) as teacher_name","max(IF(name='教室编号',value,'')) as room_number","max(IF(name='开课人数',value,'')) as opening_number","max(IF(name='最满人数',value,'')) as max_number",
                    "max(IF(name='是否可预约',value,'')) as is_booking","max(IF(name='上课次数',value,'')) as class_count"])->from('{{%goods_attr}}')->groupBy('goods_id')->having(['goods_id'=>$good->id])->one(Fun::getSiteDb());
                //$array = Fun::asArray($arrange,['course_name','teacher_name','room_number','class_day','start_time','end_time']);
            $str = '';
            $times = GoodsClassTime::find()->where(['goods_id'=>$goodid])->asArray()->all();
            if($times){
                foreach($times as $t){
                    $str .= Util::getWeek($t['class_day']).$t['start_time'].'-'.$t['end_time'];
                }
            }
            $goodsarr_query['class_day'] = $str;
            return $goodsarr_query;
        }else{
            //实现其他类型商品的特殊数据
            return null;
        }
    }
}